/*
 * File:    HttpStatistics.java
 * Created: 29-Dec-2006
 * Version: $Id$
 *
 * COPYRIGHT (C) 2006, Bitgate Software, LLC.  All Rights Reserved.
 * RELEASED UNDER THE CREATIVE COMMONS SHAREALIKE 2.5 LICENSE
 *
 * kenji.hollis@bitgatesoftware.com
 */

package com.bitgate.util.service.protocol.http;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.bitgate.util.statistics.Statistics;
import com.bitgate.util.statistics.StatisticsInterface;

/**
 * This class is used to hold statistics for requests that were made to the HTTP server.  Once a request is made, the
 * statistics class parses the <code>HttpClientContext</code> for information related to that request.  Once the data
 * is gathered, an entry is added to the <code>ConcurrentLinkedQueue</code> object, containing each line of the request
 * that was made.  This request is in a parsable format that can be used by other utilities to gather information about
 * the server request activity.  It uses the <code>bitgate.util.statistics</code> class to output statistics, so the
 * frequency of said output is based on the frequency of the output of the statistics engine.
 * <p></p>
 * The purpose for said information is so an HTTP logging package can go into a directory of sorts, read out the
 * information based on the request made by the clients that visited a specific site, and gather site statistics about
 * it based on that output.
 * <p></p>
 * There is a fundamental design flaw with this.  The idea is, I want to be able to make these statistics available for
 * any type of system.  This means, if I want to output logs to an SQL server, I should be able to.  Instead of this
 * code, there should be a replacement that uses a logging factory class to handle that.  (Please see the ROADMAP for
 * more information regarding this.)
 *
 * @author Kenji Hollis &lt;kenji@bitgatesoftware.com&gt;
 * @version $Id$
 */
public class HttpStatistics implements StatisticsInterface
{
    private static final HttpStatistics _default = new HttpStatistics();
    private final ConcurrentLinkedQueue<String> requests;

    private HttpStatistics()
    {
		this.requests = new ConcurrentLinkedQueue<String>();
		Statistics.getDefault().register(this);
    }

    /**
     * Returns the default instance of this class.
     *
     * @return <code>static HttpStatistics</code> containing the default instance.
     */
    public static HttpStatistics getDefault()
    {
    	return _default;
    }

    /**
     * Returns a list of statistics that was stored since the last statistics request.
     * 
     * @return <code>ArrayList&lt;String&gt;</code> containing the statistics.
     */
    public ArrayList<String> getStatistics()
    {
		ArrayList<String> arrayOut = new ArrayList<String>();
	
		for(String ent : this.requests) {
		    arrayOut.add(ent);
		    this.requests.remove(ent);
		}
	
		return arrayOut;
    }

    /**
     * Clears out all of the gathered statistics.
     */
    public void resetStatistics()
    {
    	this.requests.clear();
    }

    /**
     * Adds a completed request to the statistics list for parsable log file output.
     *
     * @param hcContext The <code>HttpClientContext</code> of the active client; this is the request that was made prior to
     * output to the client from the server.
     */
    public void addRequest(HttpClientContext hcContext)
    {
//		Socket sock = hcContext.getClientContext().getClientChannel().getSocketChannel().socket();
//	
//		this.requests.add(sock.getInetAddress().getHostAddress() + " - - [01/Jan/1972:00:00:00 -0700] \"" + 
//		    hcContext.getInitialRequest() + "\" " + 
//		    hcContext.getResponseCode() + " " + hcContext.getBytesSent() + " - \"" +
//		    hcContext.getHeader("user-agent") + "\"");
    }
}
